package com.example.zz.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.zz.entity.Notic;
import com.example.zz.entity.School;
import com.example.zz.service.INoticService;
import com.example.zz.service.IwService;
import com.example.zz.vo.PageVO;
import net.sf.ehcache.search.expression.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Controller
@RequestMapping("/notic")
public class NoticController {

    @Autowired
    IwService service;

    /**
     * 获取通知列表
     * @param state
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public PageVO<Notic> list(Integer state, Integer current, Integer limit){

        QueryWrapper<Notic> wrapper = new QueryWrapper<>();
        // 所有请求都需要schoolid, 暂时手动指定, 后期从登录用户对象中取出
        wrapper.eq("school_id", 1);
        wrapper.eq("state", state);

        return service.noticGetList(wrapper, current, limit);
    }

    @PostMapping("/add")
    public R<String> add(Notic notic){
        //暂时手动指定学校id
        notic.setSchoolId(1L);
        boolean result = service.noticAdd(notic);
        if(!result){
            return R.failed("新增通知失败");
        }
        return R.ok(null);
    }

    @PutMapping("/edit")
    public R<String> edit(Notic notic, Integer noticId){

        boolean result = service.noticEdit(notic, noticId);
        if(!result){
            return R.failed("失败");
        }
        return R.ok(null);
    }

    @DeleteMapping("/delete")
    public R<String> delete(Integer id){
        if(!service.noticDelete(id)){
            return R.failed("失败");
        }
        return R.ok(null);
    }

}

